using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace SalesAutomationBL
{
    public class CQueryDataBL
    {
        SalesAutomationDAL.CQueryDataDAL objQueryDAL;
        private string _XMLValue;
        public CQueryDataBL()
        {
            objQueryDAL = new SalesAutomationDAL.CQueryDataDAL();
        }

        #region method calls for SP or Table
        public string GetTableData(string strTableName)
        {
            DataSet ds = new DataSet();

            try
            {
                ds = objQueryDAL.GetQueryData(strTableName, CommandType.Text);
                GetXMLData(ds, objQueryDAL.Title);
                return _XMLValue;
            }
            catch (Exception ex)
            {
                CExceptionHandling.CCustomException exp = new CExceptionHandling.CCustomException(ex);
                return exp.XMLResponseCode;
            }
        }

        public string GetSPData(string strParameterList)
        {
            DataSet ds = new DataSet();
            try
            {
                ds = objQueryDAL.GetQueryData(strParameterList, CommandType.StoredProcedure);
                if (!objQueryDAL.IsCustomXML)
                    GetXMLData(ds, objQueryDAL.Title);
                else
                {
                    CQueryFormDetailsBL objDtls = new CQueryFormDetailsBL();
                    _XMLValue = objDtls.GetXMLData(strParameterList, ds);
                }
                    ; //TODO: Write new method that are not used in list
                return _XMLValue;
            }
            catch (Exception ex)
            {
                CExceptionHandling.CCustomException exp = new CExceptionHandling.CCustomException(ex);
                return exp.XMLResponseCode;
            }
        }

        #endregion

        #region "Create XML"
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private void GetXMLData(DataSet ds, String title)
        {
            StringBuilder sb = new StringBuilder();

            //Generate the xml string 
            sb.Append("<DATA>");
            sb.Append((new CExceptionHandling.CCustomException()).XMLResponseCode);
            sb.Append(GetXMLBase(ds, title));
            sb.Append(GetHeaders(ds));
            sb.Append(GetRows(ds));
            sb.Append(GetLinks());
            sb.Append("</DATA>");

            // Return the XML created
            _XMLValue = sb.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetXMLBase(DataSet dsTemp, string title)
        {
            return "<Title><![CDATA["+title+"]]></Title>";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetHeaders(DataSet dsTemp)
        {
            StringBuilder sbHeader = new StringBuilder();
            sbHeader.Append("<Headers>");

            int iCount = 1;
            foreach (DataColumn dc in dsTemp.Tables[0].Columns)
            {
                if (dc.ColumnName == "PKID")
                {
                    iCount++;
                    continue;
                }
                sbHeader.Append("<Column");
                sbHeader.Append(" id=\"").Append(iCount.ToString()).Append("\"");
                sbHeader.Append(" width=\"").Append(System.Configuration.ConfigurationSettings.AppSettings["HeaderColumnWidth"].ToString()).Append("\"");
                sbHeader.Append(">");
                sbHeader.Append("<![CDATA[").Append(dc.ColumnName).Append("]]>");
                sbHeader.Append("</Column>");
                iCount++;
            }
            sbHeader.Append("</Headers>");

            return sbHeader.ToString();

        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetRows(DataSet dsTemp)
        {
            StringBuilder sbRow = new StringBuilder();
            sbRow.Append("<Rows>");

            int iCount = 1;
            foreach (DataRow dr in dsTemp.Tables[0].Rows)
            {
                sbRow.Append("<Row");
                sbRow.Append(" id=\"").Append(iCount.ToString()).Append("\"");
                sbRow.Append(" key=\"").Append(dr["PKID"].ToString()).Append("\" >");
                sbRow.Append(AddColumnDetails(dr));
                sbRow.Append("</Row>");
                iCount++;
            }
            sbRow.Append("</Rows>");

            return sbRow.ToString();

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string AddColumnDetails(DataRow dr)
        {
            StringBuilder sbColumn = new StringBuilder();

            int iCount = 1;
            foreach (object dc in dr.ItemArray)
            {
                if (dr.Table.Columns[iCount - 1].ColumnName == "PKID")
                {
                    iCount++;
                    continue;
                }
                sbColumn.Append("<Column");
                sbColumn.Append(" id=\"").Append(iCount.ToString()).Append("\" >");
                sbColumn.Append("<![CDATA[").Append(dc.ToString()).Append("]]>");
                sbColumn.Append("</Column>");
                iCount++;
            }
            return sbColumn.ToString();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private string GetLinks()
        {
            //return string.Empty;

            StringBuilder sbLinks = new StringBuilder();

            sbLinks.Append("<Links>");
//            sbLinks.Append("<Link id=\"view\" data=\"\">View</Link>");
            sbLinks.Append("<Link id=\"viewicon\" data=\"\">View</Link>");
            sbLinks.Append("</Links>");
            return sbLinks.ToString();
        }
        #endregion
    }
}
